//
//  SociaReleaseTextHeaderView.m
//  ShowYork
//
//  Created by hxh on 2020/9/28.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "MineCreateOpusHeaderView.h"
@interface MineCreateOpusHeaderView ()<UITextViewDelegate>
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UITextView *textView;
@property (nonatomic,strong)UILabel *maxCount;
@property (nonatomic,strong)UILabel *placeLb;
@end

@implementation MineCreateOpusHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
        
    self.containView = [[UIView alloc]init];
    [self addSubview:self.containView];
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.bottom.equalTo(self).offset(-30);
        make.height.mas_equalTo(IPHONE_X?200:150).priorityHigh();
    }];
    
    self.textView = [[UITextView alloc]init];
    self.textView.font = font(14);
    self.textView.textColor = C51;
    self.textView.delegate = self;
    self.textView.showsVerticalScrollIndicator = NO;
    self.textView.showsHorizontalScrollIndicator = NO;
    [self.containView addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.containView).offset(10);
        make.bottom.right.equalTo(self.containView).offset(-10);
    }];
    
    self.placeLb = [[UILabel alloc]init];
    self.placeLb.text = @"给你的作品写个标题吧～";
    self.placeLb.textColor = RGBA(204, 204, 204, 1);
    self.placeLb.font = font(14);
    [self.containView addSubview:self.placeLb];
    [self.placeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containView).offset(18);
        make.left.equalTo(self.containView).offset(16);
    }];
    
    self.maxCount = [[UILabel alloc]init];
    self.maxCount.text = @"0/500";
    self.maxCount.textColor = RGBA(118, 122, 134, 1);
    self.maxCount.font = font(14);
    [self.containView addSubview:self.maxCount];
    [self.maxCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containView.mas_bottom);
        make.right.equalTo(self.containView).offset(-16);
    }];

    self.backgroundColor = [UIColor whiteColor];
    self.textView.backgroundColor = [UIColor whiteColor];
    
 
}
- (void)beginTextView{
    [self.textView becomeFirstResponder];
}

- (void)textViewDidChange:(UITextView *)textView{
    self.placeLb.hidden = textView.text.length > 0;
    
    NSString *toBeString = textView.text;
 
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
 
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > 500){
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:500];
            if (rangeIndex.length == 1){
                textView.text = [toBeString substringToIndex:500];
                if (toBeString.length > 500) {
                    [WHToast showMessage:@"已超过字数上限" duration:2 finishHandler:nil];
                }
                
            }else{
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0,500)];
                textView.text = [toBeString substringWithRange:rangeRange];
           
                if (rangeRange.length < toBeString.length) {
                    [WHToast showMessage:@"已超过字数上限" duration:2 finishHandler:nil];
                }
            }
        }
    }
    self.maxCount.text = [NSString stringWithFormat:@"%lu/500",MIN(500, (unsigned long)textView.text.length)];
    if (self.delegate && [self.delegate respondsToSelector:@selector(getOpusContentText:)]) {
        [self.delegate getOpusContentText:textView.text];
    }
}


- (void)setContent:(NSString *)content{
    self.textView.text = content;
    self.placeLb.hidden = content.length > 0;
    self.maxCount.text = [NSString stringWithFormat:@"%lu/500",MIN(500, content.length)];
}


- (void)coverMethod{
   
}




@end
